package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocCommand;
import com.cskaoyan.wordmemorize.request.VocPageRequest;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import com.cskaoyan.wordmemorize.service.VocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/voc")
public class AdminVocController {


    @Autowired
    VocService vocService;

    @Autowired
    FileUploadService fileUploadService;


    // 新增单词
    @PostMapping("")
    public Result addVoc(@RequestBody VocCommand vocCommand) {

        vocService.addVoc(vocCommand);

        return Result.ok();
    }

    // 修改单词
    @PutMapping("")
    public Result updateVoc(@RequestBody VocCommand vocCommand) {

        vocService.updateVoc(vocCommand);

        return Result.ok();
    }

    //批量删除单词
    @DeleteMapping("")
    public Result<List<DeleteVocOperateDTO>> deleteVocabularies(@RequestBody BatchDeleteCommand command) {

        List<DeleteVocOperateDTO> result = vocService.deleteVocabularies(command);

        return Result.ok(result);
    }

    // 删除指定单词
    @DeleteMapping("/{vocId}")
    public Result<DeleteVocOperateDTO> deleteVoc(@PathVariable Long vocId) {
        DeleteVocOperateDTO result = vocService.deleteVocabulary(vocId);
        return Result.ok(result);
    }

    // 删除单词与书籍章节之间的关系
    @DeleteMapping("/relation/{id}")
    public Result deleteVocRelation(@PathVariable Long id) {
        return Result.ok(vocService.deleteVocRelation(id));
    }

    // 删除单词释义
    @DeleteMapping("/interpretation/{id}")
    public Result deleteInterpretation(@PathVariable Long id) {
        return Result.ok(vocService.deleteInterpretation(id));
    }

    // 删除单词例句
    @DeleteMapping("/example/{id}")
    public Result deleteExample(@PathVariable Long id) {
        return Result.ok(vocService.deleteExample(id));
    }

    // 分页查询单词列表
    @GetMapping("/list")
    public Result<PageDTO<VocDTO>> getVocList(VocPageRequest pageRequest) {
        PageDTO<VocDTO> page = vocService.getVocabularies(pageRequest);
        return Result.ok(page);
    }

    // 删除单词音频文件
    @DeleteMapping("/file/{type}/{id}")
    public Result<DeleteVocFileOperateDTO> deleteVocAudio(@PathVariable String type, @PathVariable Long id) {
        DeleteVocFileOperateDTO result = vocService.deleteFile(id, type, null);
        return Result.ok(result);
    }

    // 上传单词音频文件
    @PostMapping("/file")
    public Result<String> uploadVocAudio(MultipartFile file) throws IOException {
        String url = fileUploadService.upload(file);
        return Result.ok(url);
    }

    @PostMapping("/excel/validate")
    public Result<Boolean> validateExcel(@RequestParam("vocExcel") MultipartFile file){
        try {
            vocService.vocExcelValidate(file);
            return Result.ok();
        } catch (IOException e) {
            return Result.fail();
        }
    }


    @PostMapping("/excel")
    public Result<ExcelAddOperateDTO> uploadExcel(@RequestParam("vocExcel")MultipartFile file){
        try {
            // 获取当前登录的后台用户id
            Long employeeId = StpKit.ADMIN.getLoginIdAsLong();
            // 调用该方法完成Excel上传单词功能
            vocService.addVocExcel(file, employeeId);
            return Result.ok();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 获取指定单词的单词-章节-书籍关系列表
    @GetMapping("/relations/{vocId}")
    public Result<List<VocChapBookDTO>> getVocRelations(@PathVariable Long vocId) {
        List<VocChapBookDTO> list = vocService.getVocRelations(vocId);
        return Result.ok(list);
    }

}
